﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNET4Study
{
    class LookupStudy
    {
        public static void Execute()
        {
            List<Student> list = new List<Student>(){
                    new Student(){ID=1,Name="zhangsan",ClassName="NT01"},
                    new Student(){ID=2,Name="lisi",ClassName="NT02"},
                    new Student(){ID=3,Name="wangwu",ClassName="NT02"},
                    new Student(){ID=4,Name="zhaoliu",ClassName="NT01"},
                    new Student(){ID=5,Name="jiangqi",ClassName="NT03"},
                    new Student(){ID=6,Name="zhouba",ClassName="NT01"},
                };

            ////1.传统的GroupBy方法是延迟执行的，即不是在调用GroupBy的时候对数据源进行分组，
            //// 而是在使用分组后的某个具体组时，才真正对集合中的内容进行分组。
            //// (以下代码虽然是先对集合进行分组，再对集合进行某些删除，但是分组的内容还是被改变了)
            //var groups= list.GroupBy(student=>student.ClassName);
            //list.RemoveAll(student=>student.ClassName=="NT02");

            //2.稳定的分组（非延迟，不可变更内容的分组是Lookup）
            var groups = list.ToLookup(student=>student.ClassName);
            list.RemoveAll(student => student.ClassName == "NT02");

            foreach (var group in groups)
            {
                Console.WriteLine(group.Key);
                foreach (var item in group)
                {
                    Console.WriteLine("\t" + item.Name);
                }
            }

            //list.Any
        }
    }
}
